package service

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"reggie/common"

	"reggie/bean"
)

func Test(c *gin.Context) {
	fmt.Println("Test func done...")
	c.JSON(200, gin.H{
		"data": nil,
		"msg":  "test ok  ->  /v1/test路由的GET请求测试成功。。。",
		"code": 200,
	})
}

func Test2(c *gin.Context) {
	fmt.Println("Test2 done ...")
	//c.String(200, "Test2 done... ...")
	c.JSON(200, bean.Result{
		Code: 1,
		Msg: "test ok",
		Data: bean.Result{
			Code: 11,
			Msg: "test ok111",
			Data: nil,
		},
	})
}

func TestSql1(c *gin.Context) {

	// 查
	var user bean.User
	common.Db.First(&user)
	fmt.Println("user = ", user)

	var users []bean.User
	common.Db.Find(&users, "status=0")
	fmt.Println("users=", users)
	common.Db.Find(&users, "status=1")
	fmt.Println("users = ", users)

	// 增
	avatar := "www.baidu.com/image/a.jpg"
	create := common.Db.Create(&bean.User{Id: 0001, Name: "User0001", Phone: "13100002222", Sex: 0,
		IdNumber: 110234567890987654, Avatar: &avatar, Status: 1})
	// create不为nil则说明报错了
	fmt.Println("create=",create.Error)
	fmt.Printf("create === %v \n", create)

	// 改
	common.Db.Find(&user, "id=1")
	fmt.Println("user1 = ", user)
	common.Db.Model(&user).Update("status", 1)

	// 删
	//common.Db.Find(&user, "id=1")
	//common.Db.Delete(&user)

	// 逻辑删除



	defer common.Db.Close() // 关闭连接
}
